# Survival Timeline by Death Reason
format_bin <- function(x) {
x <- gsub("\\(", "", x)
x <- gsub("\\]", "", x)
x <- gsub("\\[", "", x)
x <- gsub("\\)", "", x)
x <- gsub(",", "-", x)
x
}
game_data_binned <- game_data_clean %>%
mutate(duration_bin = cut(game_duration,
breaks = seq(0, 140, by = 5),
include.lowest = TRUE)) %>%
filter(!is.na(duration_bin)) %>%
mutate(duration_label = format_bin(as.character(duration_bin))) %>%
count(duration_label, death_reason, name = "count")
duration_levels <- format_bin(as.character(levels(cut(seq(0, 100, by = 5),
breaks = seq(0, 140, by = 5),
include.lowest = TRUE))))
game_data_binned$duration_label <- factor(game_data_binned$duration_label,
levels = duration_levels)
timeline_plot <- ggplot(
game_data_binned,
aes(
x = duration_label,
y = count,
color = death_reason,
group = death_reason,
text = paste0(
"<b>Death Reason:</b> ", death_reason, "<br>",
"<b>Duration:</b> ", duration_label, " sec<br>",
"<b>Count:</b> ", count
)
)
) +
geom_line(size = 0.7) +
geom_point(size = 1.5) +
labs(
title = "Survival Timeline by Death Reason",
x = "Game Duration (seconds)",
y = "Number of Deaths",
color = "Death Reason"
) +
theme_minimal() +
theme(
axis.text.x = element_text(angle = 45, hjust = 1, margin = margin(t = 5)),
axis.text.y = element_text(margin = margin(r = 5))
)
ggplotly(timeline_plot, tooltip = "text") %>%
layout(
title = list(
text = "<b>Survival Timeline by Death Reason</b>",
x = 0.5,
xanchor = "center",
font = list(size = 17)
),
legend = list(
orientation = "h",
x = 0.5,
xanchor = "center",
y = -0.25,
yanchor = "top"
),
xaxis = list(
title_standoff = 20
),
yaxis = list(
title_standoff = 20
),
margin = list(b = 160)
)